import axios from "axios";
import Vue from 'vue'

const rmsAxios = axios.create();

function isIgnoreTokenUrl (url) {
    return IGNORE_TOKEN_URLS.has(url);
}

const HTTP_ERRORS = {
    400: "参数错误",
    401: "请重新登录",
    403: "拒绝访问",
    404: "资源不存在",
    500: "内部错误",
    501: "不支持的方法",
    502: "网关错误",
    504: "网关超时"
};

const SKIP_CODES = [-10341, 10010, 10205, 1200, 12531, 173009];

function addAuthorizationSearch (config) {
  let headers = config.headers;
  let token = sessionStorage.getItem("token");
  if (token) {
    headers["Authorization"] = "Bearer " + token;
  } else {
    config.data = config.data
  }
}

function isSuccessCode ({ data }) {
    return data?.code === 0 || !data?.code || SKIP_CODES.includes(data?.code);
}

const resolveTokenError = _.debounce((Vue, config) => {
    Vue.prototype.$message({
        message: "登录已失效",
        type: "error"
    });
    const timer = setTimeout(() => {
    clearTimeout(timer);
        Vue.prototype.writeStorage("REDIRECT_URL", window.location.href);
        window.location.href = "/login";
    }, 500);
}, 200);
// axios默认配置
rmsAxios.defaults.timeout = 60000; // 超时时间
rmsAxios.defaults.baseURL = process.env.VUE_APP_URL;

// http request 拦截器
rmsAxios.interceptors.request.use(
  (config) => {
    config.headers["Content-Type"] = "application/json;charset=UTF-8";
    config.headers["Access-Control-Allow-Origin"] = "*";
    config.headers["Access-Control-Allow-Header"] = "Content-Type,*";
    addAuthorizationSearch(config);
    return config;
  },
  (error) => {
    return Promise.reject(error.response);
  }
);

rmsAxios.interceptors.response.use(
    (response) => {
      const { data, config } = response;
      if (401 === data?.code) {
        resolveTokenError(Vue, config);
        return response;
      }
      if (403 === data?.code) {
        return response;
      }
      if (isSuccessCode(response)) {
        return response;
      }
      Vue.prototype.$message({
        message: data?.message || '服务端异常',
        type: "error"
      });
      return Promise.reject(response);
    },
    (error) => {
      if (error.response) {
        // 根据请求失败的http状态码去给用户相应的提示
        if (error.response.status == 401) {
            resolveTokenError(Vue, error.response);
        } else {
            let message =
                HTTP_ERRORS[error.response?.status] || error.response?.data?.message;
            Vue.prototype.$message({
                message,
                type: "error"
            });
        }
        return Promise.reject(error);
      }
    }
  );

// 报表通用
export default {
  rmsList (params,menuId) {
    let url
    switch (menuId) {
            case 311:
                url=`/report/customer/turnoverList`
                break
            case 312:
                url=`/report/customer/turnoverMonthList`
                break
            case 313:
                url=`/report/customer/accountsReceivableClosingList`
                break
            case 314:
                url=`/report/customer/signRateList`
                break
            case 315:
                url=`/report/customer/receiptRateList`
                break
            case 316:
                url=`/report/customer/deviceBindingRateList`
                break
            case 317:
                url=`/report/customer/timeLimitForSigningList`
                break
            case 318:
                url=`/report/customer/comprehensiveAssessmentList`
                break
            case 367:
                url=`/report/carrier/signExceptionMonth`
                break
            case 368:
                url=`/report/customer/signExceptionMonth`
                break
            case 321:
                url=`/report/carrier/carrierCostsList`
                break
            case 322:
                url=`/report/carrier/carrierConsigneeCostList`
                break
            case 323:
                url=`/report/carrier/carrierShippingMethodCostsList`
                break
            case 324:
                url=`/report/carrier/carrierShippingModeCostsList`
                break
            case 325:
                url=`/report/carrier/carrierFeeTypeCostList`
                break
            case 326:
                url=`/report/carrier/carrierCostMonthList`
                break
            case 327:
                url=`/report/carrier/carrierAccountsReceivableClosingList`
                break
            case 328:
                url=`/report/carrier/carrierSignRateList`
                break
            case 329:
                url=`/report/carrier/carrierReceiptRateList`
                break
            case 330:
                url=`/report/carrier/carrierDeviceBindingRateList`
                break
            case 331:
                url=`/report/carrier/carrierTimeLimitForSigningList`
                break
            case 332:
                url=`/report/carrier/carrierComprehensiveAssessmentList`
                break
            case 333:
                url=`/report/profits/exceptionalCostProfitsList`
                break
            case 334:
                url=`/report/profits/noExceptionalCostProfitsList`
                break
            case 335:
                url=`/report/profits/shipperProfitList`
                break
            case 336:
                url=`/report/profits/realityProfitsDetailsList`
                break
            case 357:
                url=`/report/top/freightSummaryList`
                break
            case 358:
                url=`/report/top/receiptRegistrationList`
                break
            case 361:
                url=`/report/top/getTopMonthKpi`
                break
            case 36188:
                url=`/report/top/getTopMonthKpiDetail`
                break
            case 36101:
                url=`/report/top/KpiBatching`
                break
    }
    return rmsAxios.post(url, params);
  },
  exceptionList (params,type) {
    let url
    switch (type) {
            case '1':
                url=`/report/top/abnormalDeliveryList`
                break
            case '2':
                url=`/report/top/signExceptionList`
                break
    }
    return rmsAxios.post(url, params);
  },
  checkingList (params,type) {
    let url
    switch (type) {
            case '1':
                url=`/report/top/dealerStatementList`
                break
            case '2':
                url=`/report/top/mainLineStatementList`
                break
            case '3':
                url=`/report/top/oemStatementList`
                break
            case '4':
                url=`/report/top/warehouseList`
                break
    }
    return rmsAxios.post(url, params);
  },
  consignerSearchs (params) {
    let url
    url=`report/profits/getCustomerNameList`
    return rmsAxios.post(url, params);
  },
  carrierSearchs (params,type) {
    let url
    switch (type) {
        case '1':
            url=`/report/top/getSupplierCompanyId`
            break
        case '2':
            url=`/report/top/getOemGysCode`
            break
        case '3':
            url=`/report/top/getOemSupplierList`
            break
    }
    return rmsAxios.post(url, params);
  },
  kpiReportExport (uuId) {    
    return rmsAxios.get(`/report/top/topExport?uuId=${uuId?uuId:''}`);
  }
};
